package com.miracle.设计模式.行为型模式.策略模式;



public class StrategyPattern {
    public static void main(String[] args) {

        //测试策略模式
        Content content = new Content();
        Strategy strategyA = new ContentStrategyA();
        content.setStrategy(strategyA);
        content.strategy();

        System.out.println("---------- 华丽的分割线 -----------");

        Strategy strategyB = new ContentStrategyB();
        content.setStrategy(strategyB);
        content.strategy();


    }
}


/**
 * 抽象策略类
 */
interface Strategy{
    /**
     * 策略方法
     */
    public void strategyMethod();
}


/**
 * 具体策略类A
 */
class ContentStrategyA implements Strategy {
    @Override
    public void strategyMethod() {
        System.out.println("策略A的方法被访问");
    }
}


/**
 * 具体策略类B
 */
class ContentStrategyB implements Strategy {
    @Override
    public void strategyMethod() {
        System.out.println("策略B的方法被访问");
    }
}


/**
 * 环境类
 */
class Content{
    private Strategy strategy;

    public Strategy getStrategy() {
        return strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    /**
     * 调用策略方法
     */
    public void strategy(){
        strategy.strategyMethod();
    }
}

